package org.example.LocationTest;

import java.util.List;

import android.app.Activity;
import android.hardware.SensorEvent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationTestActivity extends Activity implements LocationListener {
	private LocationManager mrg;
	private TextView output;
	private String best;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mrg = (LocationManager)getSystemService(LOCATION_SERVICE);
        output = (TextView)findViewById(R.id.output);
        
        log("Location providers:");
        dumpProviders();
        
        // 
        Criteria criteria = new Criteria();
        best = mrg.getBestProvider(criteria, true);
        log("\nBest provider is: " + best);
        
        log("\nLocations (starting with last known)");
        Location location = mrg.getLastKnownLocation(best);
        dumpLocation(location);
    }

    @Override
    protected void onResume()
    {
    	super.onResume();
    	// start updates
    	mrg.requestLocationUpdates(best, 15000, 1, this);
    }
    
    @Override
    protected void onPause()
    {
    	super.onPause();
    	// stop update and save power while app paused
    	mrg.removeUpdates(this);
    }
    
	private void dumpLocation(Location location) {
		// TODO Auto-generated method stub
		if(location == null)
		{
			log("\nLocation[unknown]");
		}else{
			log("\n" + location.toString());
		}
	}

	private void dumpProviders() {
		// TODO Auto-generated method stub
		List<String> providers = mrg.getAllProviders();
		for(String provider : providers){
			dumpProvider(provider);
		}
	}
	
	private void dumpProvider(String provider)
	{
		LocationProvider info = mrg.getProvider(provider);
		StringBuilder builder = new StringBuilder();
		
		builder.append("LocationProvider[");
		builder.append("name=" + info.getName());
		builder.append(", enabled=" + mrg.isProviderEnabled(provider));
		builder.append(", getAccuracy=" + info.getAccuracy());
		builder.append(", powerRequirement=" + info.getPowerRequirement());
		builder.append(", hasMonataryCosts=" + info.hasMonetaryCost());
		builder.append(", requiresCell=" + info.requiresCell());
		builder.append(", requiresNetwork" + info.requiresNetwork());
		builder.append(", requiresSatellite" + info.requiresSatellite());
		builder.append(", supportsAltitude" + info.supportsAltitude());
		builder.append(", supportsBearing" + info.supportsBearing());
		builder.append(", supportsSpeed" + info.supportsSpeed());
		builder.append("]");
		
		log(builder.toString());
	}

	private void log(String string) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		dumpLocation(location);
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onSensorChanged(SensorEvent event)
	{
		for(int i=0;i<event.values.length; i++)
		{
			
		}
	}
	
	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
}